from django.db import models
from django.contrib.auth.models import User

class MonitoringCategory(models.Model):
    """监测类别模型"""
    name = models.CharField(max_length=100, verbose_name='类别名称')
    description = models.TextField(blank=True, null=True, verbose_name='类别描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '监测类别'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return self.name

class MonitoringData(models.Model):
    """监测数据模型"""
    category = models.ForeignKey(MonitoringCategory, on_delete=models.CASCADE, related_name='data', verbose_name='监测类别')
    location = models.CharField(max_length=255, verbose_name='监测地点')
    value = models.FloatField(verbose_name='监测值')
    unit = models.CharField(max_length=50, verbose_name='单位')
    status_choices = (
        ('normal', '正常'),
        ('warning', '警告'),
        ('danger', '危险'),
    )
    status = models.CharField(max_length=20, choices=status_choices, default='normal', verbose_name='状态')
    measured_at = models.DateTimeField(verbose_name='监测时间')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='monitoring_data', verbose_name='创建人')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    notes = models.TextField(blank=True, null=True, verbose_name='备注')
    
    class Meta:
        verbose_name = '监测数据'
        verbose_name_plural = verbose_name
        ordering = ['-measured_at']
        
    def __str__(self):
        return f"{self.category.name} - {self.location} - {self.measured_at.strftime('%Y-%m-%d %H:%M')}"
